#include <SDL/SDL.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <iostream>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "include/Scene.h"
#include "include/Jeu.h"
#include "include/Monde.h"
#include "include/Territoire.h"
#include "include/Pion.h"
#include "include/De.h"
#include "include/Partie.h"

#define GETCOORD(frame, x, y) (&(theSea.coords[frame*theSea.numCoords+(x)+(y)*(theSea.widthX+1)]))
#define GETFACET(frame, x, y) (&(theSea.facets[frame*theSea.numFacets+(x)+(y)*theSea.widthX]))

// Pour le feu
int first = 1;
#define vinit(a,i,j,k) {\
  (a)[0]=i;\
  (a)[1]=j;\
  (a)[2]=k;\
}
#define vinit4(a,i,j,k,w) {\
  (a)[0]=i;\
  (a)[1]=j;\
  (a)[2]=k;\
  (a)[3]=w;\
}
#define vadds(a,dt,b) {\
  (a)[0]+=(dt)*(b)[0];\
  (a)[1]+=(dt)*(b)[1];\
  (a)[2]+=(dt)*(b)[2];\
}
#define vequ(a,b) {\
  (a)[0]=(b)[0];\
  (a)[1]=(b)[1];\
  (a)[2]=(b)[2];\
}

#define clamp(a) ((a) < 0.0 ? 0.0 : ((a) < 1.0 ? (a) : 1.0))
#define vclamp(v) {\
  (v)[0]=clamp((v)[0]);\
  (v)[1]=clamp((v)[1]);\
  (v)[2]=clamp((v)[2]);\
}
#define RIDCOL 0.4
#define AGRAV -9.8
typedef struct
{
   int age;
   float p[3][3];
   float v[3];
   float c[3][4];
} part;

float blu[3] = { 1.0, 0.2, 0.0 };
float blu2[3] = { 1.0, 1.0, 0.0 };
int np = 800;
float eject_r = -0.35;
float dt = 0.015;
float eject_vy = 4;
float eject_vl = 1;
float ridtri = .5;
float maxage = 1.0 / dt;
part *p = (part *) malloc(sizeof(part) * np);
// FIN Pour le feu

using namespace std;

struct facet 
{
      float color[3];
      float normal[3];
};
struct coord 
{
      float vertex[3];
      float normal[3];
};
struct sea 
{
      int widthX, widthY;
      int numFacets;
      int numCoords;
      int frames;
      int curFrame;
      int nextFrame;
      float height; 
      struct coord *coords;
      struct facet *facets;
      bool smooth;
      bool lighting;
} theSea;

Scene::Scene() 
   : territoireHighlighted(NULL), positionFeu(NULL)
{ 
   initAnimations();
   fog=true;
   initSea();
}

void Scene::initAnimations()
{
   animations[Animation::INTRO].state = false;
   animations[Animation::INTRO].timer = 40;
   animations[Animation::SEA].state = true;
   animations[Animation::SEA].timer = 150;
   animations[Animation::CAM].state = false;
   animations[Animation::CAM].timer = 40;
   animations[Animation::FEU].state = false;
   animations[Animation::FEU].timer = 40;
   animations[Animation::NOMJOUEUR].state = false;
   animations[Animation::NOMJOUEUR].timer = 40;
   animations[Animation::DE].state = false;
   animations[Animation::DE].timer = 40;
}

bool Scene::isAnimated()
{
   bool result = false;
   for (int i=0; i<Animation::NTYPEANIM; i++)
      result |= animations[i].state;
   return result;
}

int Scene::getTimerAnimation()
{
   int result = __INT_MAX__;
   for (int i=0; i<Animation::NTYPEANIM; i++)
      if ( animations[i].state )
         result = min(result, animations[i].timer);
   return result;
}

void Scene::startAnimation(Animation::TypeAnim _type)
{
   if ( !isAnimated() )
      glutTimerFunc (1, Jeu::timer, 1);
   animations[_type].state = true;
}

void Scene::stopAnimation(Animation::TypeAnim _type)
{
   animations[_type].state = false;
}

void Scene::toggleAnimationState(Animation::TypeAnim _type)
{
   if ( animations[_type].state )
      stopAnimation( _type );
   else
      startAnimation( _type );
}

void Scene::moveCam(int _x, int _y)
{
   camera.centerX +=  mouse.x - _x;
   camera.centerY +=  mouse.y - _y;
   camera.eyeX +=  mouse.x - _x;
   camera.eyeY +=  mouse.y - _y;
   mouse.x = _x;
   mouse.y = _y;
}

int Scene::animCameraCenterTo(void * _center)
{
   Point3D* center = (Point3D*) _center;
   Jeu::jeu->getScene()->startAnimation( Animation::CAM );
   int pas = 25;

   float center_x_start = Jeu::jeu->scene.camera.centerX;
   float center_x_dest = center_x_start + (center->x() - Jeu::jeu->scene.camera.centerX - 350.0);
   float eye_x_start = Jeu::jeu->scene.camera.eyeX;
   float eye_x_dest = eye_x_start + (center->x() - Jeu::jeu->scene.camera.eyeX - 350.0);
   float center_x_distance = center_x_dest - center_x_start;
   float eye_x_distance = eye_x_dest - eye_x_start;
  
   float center_y_start = Jeu::jeu->scene.camera.centerY;
   float center_y_dest = center_y_start + (center->y() - Jeu::jeu->scene.camera.centerY - 350.0);
   float eye_y_start = Jeu::jeu->scene.camera.eyeY;
   float eye_y_dest = eye_y_start + (center->y() - Jeu::jeu->scene.camera.eyeY - 350.0);
   float center_y_distance = center_y_dest - center_y_start;
   float eye_y_distance = eye_y_dest - eye_y_start;

   while( abs((int)center_x_distance) > pas
          || abs((int)center_y_distance) > pas )
   {
      if ( abs((int)center_y_distance) > pas )
         if ( center_y_distance > 0 )
            Jeu::jeu->scene.camera.centerY += pas;
         else
            Jeu::jeu->scene.camera.centerY -= pas;

      if ( abs((int)center_x_distance) > pas )
         if ( center_x_distance > 0 )
            Jeu::jeu->scene.camera.centerX += pas;
         else
            Jeu::jeu->scene.camera.centerX -= pas;

      if ( abs((int)eye_y_distance) > pas )
         if ( eye_y_distance > 0 )
            Jeu::jeu->scene.camera.eyeY += pas;
         else
            Jeu::jeu->scene.camera.eyeY -= pas;

      if ( abs((int)eye_x_distance) > pas )
         if ( eye_x_distance > 0 )
            Jeu::jeu->scene.camera.eyeX += pas;
         else
            Jeu::jeu->scene.camera.eyeX -= pas;

      SDL_Delay( 40 );
      center_x_distance = center_x_dest - Jeu::jeu->scene.camera.centerX;
      eye_x_distance = eye_x_dest - Jeu::jeu->scene.camera.eyeX;
      center_y_distance = center_y_dest - Jeu::jeu->scene.camera.centerY;
      eye_y_distance = eye_y_dest - Jeu::jeu->scene.camera.eyeY;
   }
   Jeu::jeu->getScene()->stopAnimation( Animation::CAM );
   return 0;
}

int animAlphaTerritoire(void * _territoire)
{
   Territoire * territoire = (Territoire *)_territoire;
   while( territoire->getAlpha() < 1.0f )
   {
      territoire->setAlpha( territoire->getAlpha() + 0.05f );
      SDL_Delay( 100 );
   }
   //clignotement
   for(int i=0; i<3; i++)
   {
      territoire->setAlpha( 0.0f );
      SDL_Delay( 80 );
      territoire->setAlpha( 1.0f );
      SDL_Delay( 80 );
   }
   return 0;
}

bool contient(std::vector<int> * _vector, int _val)
{
   std::vector<int>::const_iterator ite;
   for(ite = _vector->begin();
       ite != _vector->end();
       ite++)
   {
      if(*ite == _val)
         return true;
   }
   return false;
}

int Scene::animIntroduction(void *)
{
   Jeu::jeu->getScene()->startAnimation( Animation::INTRO );
   // animation des territoire: on remet progessivement en visible les Territoires
   vector<int> territoires_visibles;
   int cur_territoire_id;
   Territoire * cur_territoire;
   SDL_Thread * thread_animAlpha = NULL;
   srand(time(NULL));
   while( (int)territoires_visibles.size() != Jeu::jeu->getMonde()->countTerritoires() )
   {
      do
      {
         cur_territoire_id = rand() % Jeu::jeu->getMonde()->countTerritoires();
      }
      while( contient(&territoires_visibles, cur_territoire_id) );
      territoires_visibles.push_back(cur_territoire_id);
      cur_territoire = Jeu::jeu->getMonde()->getIndexedTerritoire( cur_territoire_id );
      thread_animAlpha = SDL_CreateThread(animAlphaTerritoire, cur_territoire);
      if ( thread_animAlpha == NULL ) 
      {
         printf("Unable to create thread: %s\n", SDL_GetError());
         exit(1);
      }
      SDL_Delay(200);
   }
   if ( thread_animAlpha )
      SDL_WaitThread(thread_animAlpha, NULL);
   Jeu::jeu->getScene()->stopAnimation( Animation::INTRO );
   return 0;
}

void Scene::initSea()
{
   struct coord *coord;
   struct facet *facet;
   float dp1[3], dp2[3];
   float *pt1, *pt2, *pt3;
   float angle, d, x, y;
   int numFacets, numCoords, frameNum, i, j;
   
   theSea.widthX = 10;
   theSea.widthY = 10;
   theSea.frames = 10; // vitesse de la vague
   theSea.curFrame = 0;
   theSea.nextFrame = 0;
   theSea.height = 0.1;
   theSea.smooth=false; 
   theSea.lighting=true;

   numFacets = theSea.widthX * theSea.widthY;
   numCoords = (theSea.widthX + 1) * (theSea.widthY + 1);
   
   theSea.numCoords = numCoords;
   theSea.numFacets = numFacets;
   
   theSea.coords = (struct coord *) malloc( theSea.frames * numCoords * sizeof(struct coord) );
   theSea.facets = (struct facet *) malloc( theSea.frames * numFacets * sizeof(struct facet) );
   if (theSea.coords == NULL || theSea.facets == NULL) 
   {
      printf("Out of memory.\n");
      exit(1);
   }
   
   for (frameNum = 0; frameNum < theSea.frames; frameNum++) 
   {
      for (i = 0; i <= theSea.widthX; i++) 
      {
         x = i / (float)theSea.widthX;
         for (j = 0; j <= theSea.widthY; j++) 
         {
            y = j / (float)theSea.widthY;
            
            d = sqrt(x*x+y*y);
            if (d == 0.0) 
               d = 0.0001;
            angle = 2 * M_PI * d + (2 * M_PI / theSea.frames * frameNum);
            
            coord = GETCOORD(frameNum, i, j);
            
            coord->vertex[0] = x - 0.5;
            coord->vertex[1] = y - 0.5;
            coord->vertex[2] = (theSea.height - theSea.height * d) * cos(angle);
            
            coord->normal[0] = -(theSea.height / d) * x * ((1 - d) * 2 * M_PI *
                                                    sin(angle) + cos(angle));
            coord->normal[1] = -(theSea.height / d) * y * ((1 - d) * 2 * M_PI *
                                                    sin(angle) + cos(angle));
            coord->normal[2] = -1;
            
            d = 1.0 / sqrt(coord->normal[0]*coord->normal[0]+
                           coord->normal[1]*coord->normal[1]+1);
            coord->normal[0] *= d;
            coord->normal[1] *= d;
            coord->normal[2] *= d;
         }
      }
      for (i = 0; i < theSea.widthX; i++) 
      {
         for (j = 0; j < theSea.widthY; j++) 
         {
            facet = GETFACET(frameNum, i, j);
            
            facet->color[0] = 0.0;
            facet->color[1] = 0.5;
            facet->color[2] = 1.0;
            
            pt1 = GETCOORD(frameNum, i, j)->vertex;
            pt2 = GETCOORD(frameNum, i, j+1)->vertex;
            pt3 = GETCOORD(frameNum, i+1, j+1)->vertex;
            
            dp1[0] = pt2[0] - pt1[0];
            dp1[1] = pt2[1] - pt1[1];
            dp1[2] = pt2[2] - pt1[2];
            
            dp2[0] = pt3[0] - pt2[0];
            dp2[1] = pt3[1] - pt2[1];
            dp2[2] = pt3[2] - pt2[2];
            
            facet->normal[0] = dp1[1] * dp2[2] - dp1[2] * dp2[1];
            facet->normal[1] = dp1[2] * dp2[0] - dp1[0] * dp2[2];
            facet->normal[2] = dp1[0] * dp2[1] - dp1[1] * dp2[0];
            
            d = 1.0 / sqrt(facet->normal[0]*facet->normal[0]+
                           facet->normal[1]*facet->normal[1]+
                           facet->normal[2]*facet->normal[2]);
            
            facet->normal[0] *= d;
            facet->normal[1] *= d;
            facet->normal[2] *= d;
         }
      }
   }
}

void Scene::drawSea()
{
   struct coord *coord;
   struct facet *facet;
   float *lastColor;
   float *thisColor;
   int i, j;
   
   //glClear(clearMask);
   if( animations[Animation::SEA].state )
      theSea.curFrame++;

   if (theSea.curFrame >= theSea.frames)
      theSea.curFrame = 0;
 
   theSea.nextFrame = 0;
   
   for (i = 0; i < theSea.widthX; i++) 
   {
      glBegin(GL_QUAD_STRIP);
      lastColor = NULL;
      for (j = 0; j < theSea.widthY; j++) 
      {
         facet = GETFACET(theSea.curFrame, i, j);
         if ( !theSea.smooth && theSea.lighting ) 
            glNormal3fv(facet->normal);

         thisColor = facet->color;
         glColor3fv(facet->color);
	 
         
         if ( !lastColor || (thisColor[0] != lastColor[0] && theSea.smooth) ) 
         {
            if (lastColor) 
            {
               glEnd();
               glBegin(GL_QUAD_STRIP);
            }
            coord = GETCOORD(theSea.curFrame, i, j);
            if ( theSea.smooth && theSea.lighting )
               glNormal3fv(coord->normal);
            
            glVertex3fv(coord->vertex);
            
            coord = GETCOORD(theSea.curFrame, i+1, j);
            if ( theSea.smooth && theSea.lighting )
               glNormal3fv(coord->normal);
            
            glVertex3fv(coord->vertex);
         }
         
         coord = GETCOORD(theSea.curFrame, i, j+1);
         if ( theSea.smooth && theSea.lighting )
            glNormal3fv(coord->normal);
         
         glVertex3fv(coord->vertex);
         
         coord = GETCOORD(theSea.curFrame, i+1, j+1);
         if ( theSea.smooth && theSea.lighting )
            glNormal3fv(coord->normal);
         
         glVertex3fv(coord->vertex);
                
         lastColor = thisColor;
      }
      glEnd();
   }
}

void Scene::highlightTerritoire(Territoire * _territoire)
{
   if ( territoireHighlighted )
      if ( territoireHighlighted->isHighlighted() )
         territoireHighlighted->setNotHighlighted();
   
   territoireHighlighted = _territoire;
   if ( territoireHighlighted )
      territoireHighlighted->setHighlighted();
  
}

int Scene::animNomJoueur(void*)
{
   Jeu::jeu->getScene()->startAnimation( Animation::NOMJOUEUR );
   Jeu::jeu->guinterface.positionNomJoueur->setX(-1.0f);
   Jeu::jeu->guinterface.toggleShowingNomJoueur();
   
   while( Jeu::jeu->guinterface.positionNomJoueur->x() < -0.2f )
   {
      Jeu::jeu->guinterface.positionNomJoueur->setX(Jeu::jeu->guinterface.positionNomJoueur->x()+0.02f);
      SDL_Delay( 60 );
   }
   for (int i=0; i<8; i++)
   {
      Jeu::jeu->guinterface.toggleShowingNomJoueur();
      SDL_Delay( 150 );
   }
   while( Jeu::jeu->guinterface.positionNomJoueur->x() < 1.0f )
   {
      Jeu::jeu->guinterface.positionNomJoueur->setX(Jeu::jeu->guinterface.positionNomJoueur->x()+0.02f);
      SDL_Delay( 60 );
   }

   Jeu::jeu->guinterface.toggleShowingNomJoueur();
   Jeu::jeu->getScene()->stopAnimation( Animation::NOMJOUEUR );

   return 0;
}

float vrnd(void)
{
   return (((float) rand()) / RAND_MAX);
}

void setnewpart(part * p)
{
   float a, v[3], *c;

   p->age = 0;

   a = vrnd() * M_PI * 2.0;

   vinit(v, sin(a) * eject_r * vrnd(), .15, cos(a) * eject_r * vrnd());
   vinit(p->p[0], v[0] + vrnd() * ridtri, v[1] + vrnd() * ridtri, v[2] + vrnd() * ridtri);
   vinit(p->p[1], v[0] + vrnd() * ridtri, v[1] + vrnd() * ridtri, v[2] + vrnd() * ridtri);
   vinit(p->p[2], v[0] + vrnd() * ridtri, v[1] + vrnd() * ridtri, v[2] + vrnd() * ridtri);

   vinit(p->v, v[0] * eject_vl / (eject_r / 2), vrnd() * eject_vy + eject_vy / 2, v[2] * eject_vl / (eject_r / 2));

   c = blu;

   vinit4(p->c[0], c[0] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
          c[1] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
	  c[2] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), 1.0);
   vinit4(p->c[1], c[0] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
	  c[1] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
	  c[2] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), 1.0);
   vinit4(p->c[2], c[0] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
	  c[1] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
	  c[2] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), 1.0);
}

void setpart(part * p)
{
   float fact;

   if (p->p[0][1] < 0.1) {
      setnewpart(p);
      return;
   }

   p->v[1] += AGRAV * dt;

   vadds(p->p[0], dt, p->v);
   vadds(p->p[1], dt, p->v);
   vadds(p->p[2], dt, p->v);

   p->age++;

   if ((p->age) > maxage) 
   {
      vequ(p->c[0], blu2);
      vequ(p->c[1], blu2);
      vequ(p->c[2], blu2);
   }
   else 
   {
      fact = 1.0 / maxage;
      vadds(p->c[0], fact, blu2);
      vclamp(p->c[0]);
      p->c[0][3] = fact * (maxage - p->age);

      vadds(p->c[1], fact, blu2);
      vclamp(p->c[1]);
      p->c[1][3] = fact * (maxage - p->age);

      vadds(p->c[2], fact, blu2);
      vclamp(p->c[2]);
      p->c[2][3] = fact * (maxage - p->age);
   }
}

void Scene::drawFire()
{
   glPushMatrix();
   glTranslatef(positionFeu->x(), positionFeu->y(), -10.0f);
   glRotatef(-90.0f, 1.0f, .0f, 0.0f);
   glScalef(50.0, 50.0, 50.0);
   
   glDepthMask(GL_TRUE);
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   
   if (first)
   {
      for (int i = 0; i < np; i++)
         setnewpart(&p[i]);
      first = 0;
   }
/*
   double t0 = -1.;
   double t;
   if (t0 < 0.)
      t0 = (double) glutGet(GLUT_ELAPSED_TIME) / 1000.;
   t = (double) glutGet(GLUT_ELAPSED_TIME) / 1000.;
   dt = t - t0;
   t0 = t;
*/
   glBegin(GL_TRIANGLES);
   for (int j = 0; j < np; j++) 
   {
      glColor4fv(p[j].c[0]);
      glVertex3fv(p[j].p[0]);

      glColor4fv(p[j].c[1]);
      glVertex3fv(p[j].p[1]);

      glColor4fv(p[j].c[2]);
      glVertex3fv(p[j].p[2]);

      setpart(&p[j]);
   }
   glEnd();

   glDisable(GL_BLEND);
   glPopMatrix();
}

int Scene::animFeu(void* _point)
{
   Point3D* point = (Point3D*) _point;
   if ( point )
   {
      Jeu::jeu->getScene()->setPositionFeu( point );
      Jeu::jeu->getScene()->startAnimation( Animation::FEU );
      SDL_Delay( 4000 );
      Jeu::jeu->getScene()->stopAnimation( Animation::FEU );
   }
   return 0;
}

void Scene::drawDe()
{
   glPushMatrix();
   glLoadIdentity();
   gluLookAt (0, 0, -1,
              0, 0, 0,
              0, -1, 0);
   glScalef(0.1,0.1,0.1);
   
   GLfloat light_diffuse[] = { 0.7, 0.7, 0.7 };
   GLfloat light_spectacular[] = { 0.1, 0.1, 0.1 };
   GLfloat light_position[] = { 0.0, 1.0, -20.0 };
   
   glEnable(GL_DEPTH_TEST);
   glEnable(GL_LIGHTING);
   
   glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
      //glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
   glLightfv(GL_LIGHT0, GL_SPECULAR, light_spectacular);
   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
   glEnable(GL_LIGHT0);
   
   //rotation+=10;
   glRotatef(-24.0f,1.0f,0.0f,0.0f);
   glTranslatef(-5.0,0.0,0.0);
   
   for(int i=0; i < Partie::NBDE; i++)
   {
      glPushMatrix();
      glRotatef((Jeu::jeu->getPartie()->getDeAttak()[i]).getAngleRotation(),
                   1.0f,0.0f,0.0f);
      glmDraw((Jeu::jeu->getPartie()->getDeAttak()[i]).getModel(),
                 GLM_SMOOTH | GLM_MATERIAL);
      glPopMatrix();
      glTranslatef(1.5,0.0,0.0);
   }
   
   glTranslatef(2.0,0.0,0.0);
   for(int i=0; i< Partie::NBDE;i++)
   {
       glPushMatrix();
       glRotatef((Jeu::jeu->getPartie()->getDeDef()[i]).getAngleRotation()
                 ,1.0f,0.0f,0.0f);
       glmDraw((Jeu::jeu->getPartie()->getDeDef()[i]).getModel(),
                  GLM_SMOOTH | GLM_MATERIAL);
       glPopMatrix();
       glTranslatef(1.5,0.0,0.0);
   }
    
   glDisable(GL_LIGHTING);
   glPopMatrix();
}

int Scene::animDe(void*)
{
   srand( time(NULL) );
   int temp_rot1,
      temp_rot2,
      temp_rot3, 
      temp_rot4,
      temp_rot5,
      temp_rot6;

   temp_rot1 = rand() % 100;
   temp_rot2 = rand() % 100;
   temp_rot3 = rand() % 100;
   temp_rot4 = rand() % 100;
   temp_rot5 = rand() % 100;
   temp_rot6 = rand() % 100;

   int nb = 0;
   
   Jeu::jeu->getPartie()->animDeStart();
   Jeu::jeu->getScene()->startAnimation( Animation::DE );

   while( !Jeu::jeu->getPartie()->allDeStopped() )
   {
      for(int i=0; i< Partie::NBDE; i++)
      {
         Jeu::jeu->getPartie()->getDeAttak()[i].makeRotation();
         Jeu::jeu->getPartie()->getDeDef()[i].makeRotation();
      }
      SDL_Delay(40);
      if( nb == temp_rot1)
         Jeu::jeu->getPartie()->getDeAttak()[0].toggleRotation();
      if( nb == temp_rot2)
         Jeu::jeu->getPartie()->getDeAttak()[1].toggleRotation();
      if( nb == temp_rot3)
         Jeu::jeu->getPartie()->getDeAttak()[2].toggleRotation();
      if( nb == temp_rot4)
         Jeu::jeu->getPartie()->getDeDef()[0].toggleRotation();
      if( nb == temp_rot5)
         Jeu::jeu->getPartie()->getDeDef()[1].toggleRotation();
      if( nb == temp_rot6)
         Jeu::jeu->getPartie()->getDeDef()[2].toggleRotation(); 
      nb++;
   }

   Jeu::jeu->getPartie()->resetAllDeState();
      
   Jeu::jeu->getPartie()->setResultArmy( Jeu::jeu->getPartie()->getBestScoreAttak() );
   Jeu::jeu->getPartie()->setResultEnemy( Jeu::jeu->getPartie()->getBestScoreDef() );

   Jeu::jeu->getPartie()->animDeEnd();
   return 0;
}
